package ch.zbw.sysventory.server.control.validator;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import ch.zbw.sysventory.server.model.ScanConfiguration;
import ch.zbw.sysventory.server.model.User;

@Component
public class ScanConfigurationValidator implements Validator {

	@Override
	public boolean supports(Class<?> clazz) {
		return User.class.equals(clazz);
	}

	@Override
	public void validate(Object object, Errors errors) {
		if (object instanceof User) {
			ScanConfiguration scanConfiguration = (ScanConfiguration) object;
			if (scanConfiguration.getStartIp() == null || "".equals(scanConfiguration.getStartIp())) {
				errors.rejectValue("startIp", "emptyField.scanconfiguration.startIp");
			}
			if (scanConfiguration.getEndIp() == null || "".equals(scanConfiguration.getEndIp())) {
				errors.rejectValue("endIp", "emptyField.scanconfiguration.endIp");
			}
			if (scanConfiguration.getAgentPort() < 1) {
				errors.rejectValue("agentPort", "emptyField.scanconfiguration.agentPort");
			}
		}
	}
}
